/**
 * utils函数引入
 **/
var HtmlToJson = require("./html2json.js");

/**
 * 配置及公有属性
 **/ var realWindowWidth = 0;

var realWindowHeight = 0;

wx.getSystemInfo({
    success: function(res) {
        realWindowWidth = res.windowWidth;
        realWindowHeight = res.windowHeight;
    }
})
/**
 * 主函数入口区
 **/;

function wxParse(bindName = "wxParseData", type = "html", data = '<div class="color:red;">暂无内容</div>', target, imagePadding) {
    var that = target;
    var transData = {};
    //存放转化后的数据
        if (type == "html") {
        transData = HtmlToJson.html2json(data, bindName);
        //console.log(JSON.stringify(transData, ' ', ' '));
        }
    transData.view = {};
    transData.view.imagePadding = 0;
    if (typeof imagePadding != "undefined") {
        transData.view.imagePadding = imagePadding;
    }
    var bindData = {};
    bindData[bindName] = transData;
    that.setData(bindData);
    that.wxParseImgTap = wxParseImgTap;
    that.wxParseTagATap = wxParseTagATap;
}

//图片链接调整
function wxParseTagATap(e) {
    var that = this;
    var url = e.currentTarget.dataset.src;
    if (url != "") {
        wx.navigateTo({
            url: url
        });
    }
}

// // 图片点击事件
function wxParseImgTap(e) {
    var that = this;
    var nowImgUrl = e.target.dataset.src;
    wx.previewImage({
        current: nowImgUrl,
        // 当前显示图片的http链接
        urls: [ nowImgUrl ]
    });
}

function wxParseTemArray(temArrayName, bindNameReg, total, that) {
    var array = [];
    var temData = that.data;
    var obj = null;
    for (var i = 0; i < total; i++) {
        var simArr = temData[bindNameReg + i].nodes;
        array.push(simArr);
    }
    temArrayName = temArrayName || "wxParseTemArray";
    obj = JSON.parse('{"' + temArrayName + '":""}');
    obj[temArrayName] = array;
    that.setData(obj);
}

/**
 * 配置emojis
 * 
 */ function emojisInit(reg = "", baseSrc = "/wxParse/emojis/", emojis) {
    HtmlToJson.emojisInit(reg, baseSrc, emojis);
}

module.exports = {
    wxParse: wxParse,
    wxParseTemArray: wxParseTemArray,
    emojisInit: emojisInit
};